OPENAI规格 API接口使用说明

基本信息

  • 模型名称: Qwen2.5-VL-32B-Instruct (多模态视觉语言模型)
  • API URL: https://openai/v1/chat/completions
  • 请求方法: POST

认证方式

Bearer Token

在请求头 (Header) 中加入 Authorization 字段进行认证。

Authorization: Bearer your_api_key_here

请求示例

1. 纯文本对话

curl -X POST "https://openai/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d '{
    "model": "qwen2.5-v1-32b-instruct",
    "messages": [
        {
            "role": "user",
            "content": "你好"
        }
    ]
}'

2. 图像理解 (Base64编码)```bash

curl -X POST "https://openai/v1/chat/completions"
-H "Content-Type: application/json"
-H "Authorization: Bearer your_api_key_here"
-d '{ "model": "qwen2.5-v1-32b-instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "请描述这张图片的内容" }, { "type": "image_url", "image_url": { "url": "image/path" } } ] } ] }'

### 3. 图像理解 (URL方式)
```bash
curl -X POST "https://openai/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d '{
    "model": "qwen2.5-v1-32b-instruct",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片展示了什么?"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://example.com/image.jpg"
                    }
                }
            ]
        }
    ]
}'```

### 4. 多轮对话
```bash
curl -X POST "https://openai/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d '{
    "model": "qwen2.5-v1-32b-instruct",
    "messages": [
        {
            "role": "system",
            "content": "你是一个专业的图像分析助手"
        },
        {
            "role": "user",
            "content": "你好"
        },
        {
            "role": "assistant",
            "content": "你好! 我是一个专业的图像分析助手,可以帮你分析和理解图片内容。"
        },
        {
            "role": "user",
            "content": "请帮我分析这张图片中的物体"
        }
    ]
}'

5. 流式响应

通过将 stream 参数设置为 true 来实现流式响应。

curl -X POST "https://openai/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d '{
    "model": "qwen2.5-v1-32b-instruct",
    "messages": [
        {
            "role": "user",
            "content": "请写一个关于人工智能的短文"
        }
    ],
    "stream": true
}' \
--no-buffer

6. 图像+文本复合分析

curl -X POST "https://openai/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d '{
    "model": "qwen2.5-v1-32b-instruct",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "请分析这张图片中的技术架构,并提供改进建议:"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..."
                    }
                }
            ]
        }
    ]
}'

7. 使用API Key参数认证

除了 Bearer Token,也支持通过 URL 参数传递 API Key。

curl -X POST "https://openai/v1/chat/completions?apikey=your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen2.5-v1-32b-instruct",
    "messages": [
        {
            "role": "user",
            "content": "你好,请介绍一下你的功能"
        }
    ]
}'

8. 本地图片转Base64示例

# 首先将本地图片转换为Base64
IMAGE_BASE64=$(base64 -i /path/to/your/image.jpg)

# 然后发送请求
curl -X POST "https://openai/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_api_key_here" \
-d "{
    \"model\": \"qwen2.5-v1-32b-instruct\",
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": [
                {
                    \"type\": \"text\",
                    \"text\": \"请分析这张图片\"
                },
                {
                    \"type\": \"image_url\",
                    \"image_url\": {
                        \"url\": \"data:image/jpeg;base64,${IMAGE_BASE64}\"
                    }
                }
            ]
        }
    ]
}"

请求参数说明

必需参数

  • model (string): 模型名称, 固定为 "qwen2.5-v1-32b-instruct"
  • messages (array): 对话消息数组, 详见 Messages 格式说明。

可选参数

  • temperature (float): 控制随机性,值越高随机性越强。
  • max_tokens (integer): 最大生成token数。
  • stream (boolean): 是否流式返回。
  • top_p (float): 核采样参数。
  • frequency_penalty (float): 频率惩罚。
  • presence_penalty (float): 存在惩罚。
  • stop (string|array): 停止词。

Messages 格式

纯文本消息

{
    "role": "user",
    "content": "你的问题"
}

多模态消息 (文本+图像)

{
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": "请分析这张图片"
        },
        {
            "type": "image_url",
            "image_url": {
                "url": "data:image/jpeg;base64,base64_encoded_image"
            }
        }
    ]
}

支持的图像格式

  • JPEG
  • PNG
  • WebP
  • GIF

响应格式

标准响应

{
    "id": "chatcmpl-xxx",
    "object": "chat.completion",
    "created": 1234567890,
    "model": "qwen2.5-v1-32b-instruct",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "模型的回复内容"
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 10,
        "completion_tokens": 20,
        "total_tokens": 30
    }
}

流式响应

stream: true 时, 响应为 Server-Sent Events 格式:

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen25-v1-32b-instruct","choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1234567890,"model":"qwen25-v1-32b-instruct","choices":[{"index":0,"delta":{"content":"好"},"finish_reason":null}]}

data: [DONE]

错误处理

常见错误码

  • 400: 请求参数错误
  • 401: 认证失败
  • 429: 请求频率超限
  • 500: 服务器内部错误
  • 502/503/504: 服务暂时不可用

错误响应格式

{
    "error": {
        "message": "错误描述",
        "type": "invalid_request_error",
        "code": "invalid_api_key"
    }
}